package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ISystemHelpService;
import com.mdd.admin.validate.SystemHelpCreateValidate;
import com.mdd.admin.validate.SystemHelpUpdateValidate;
import com.mdd.admin.validate.SystemHelpSearchValidate;
import com.mdd.admin.vo.SystemHelpListedVo;
import com.mdd.admin.vo.SystemHelpDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.SystemHelp;
import com.mdd.common.mapper.SystemHelpMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 帮助中心实现类
 * @author DEREK
 */
@Service
public class SystemHelpServiceImpl implements ISystemHelpService {
        
    @Resource
    SystemHelpMapper systemHelpMapper;

    /**
     * 帮助中心列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<SystemHelpListedVo>
     */
    @Override
    public PageResult<SystemHelpListedVo> list(PageValidate pageValidate, SystemHelpSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<SystemHelp> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        systemHelpMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:title:str",
            "=:content:str",
            "=:browseNum@browse_num:int",
            "=:extend:str",
        });

        IPage<SystemHelp> iPage = systemHelpMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<SystemHelpListedVo> list = new LinkedList<>();
        for(SystemHelp item : iPage.getRecords()) {
            SystemHelpListedVo vo = new SystemHelpListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 帮助中心详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return SystemHelp
     */
    @Override
    public SystemHelpDetailVo detail(Integer id) {
        SystemHelp model = systemHelpMapper.selectOne(
                new QueryWrapper<SystemHelp>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        SystemHelpDetailVo vo = new SystemHelpDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 帮助中心新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(SystemHelpCreateValidate createValidate) {
        SystemHelp model = new SystemHelp();
        model.setTitle(createValidate.getTitle());
        model.setContent(createValidate.getContent());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setBrowseNum(createValidate.getBrowseNum());
        model.setExtend(createValidate.getExtend());
        systemHelpMapper.insert(model);
    }

    /**
     * 帮助中心编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(SystemHelpUpdateValidate updateValidate) {
        SystemHelp model = systemHelpMapper.selectOne(
                new QueryWrapper<SystemHelp>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setTitle(updateValidate.getTitle());
        model.setContent(updateValidate.getContent());
        model.setBrowseNum(updateValidate.getBrowseNum());
        model.setExtend(updateValidate.getExtend());
        systemHelpMapper.updateById(model);
    }

    /**
     * 帮助中心删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        SystemHelp model = systemHelpMapper.selectOne(
                new QueryWrapper<SystemHelp>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        systemHelpMapper.delete(new QueryWrapper<SystemHelp>().eq("id", id));
    }

}
